home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / ScreenSavers / BackSpaceViews / GradientView.BackModule / GradientView.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  5.0 KB  |  256 lines

  1. /***
  2.     GradientView by J. Shan Bell
  3.     Nov 7, 1993
  4.     v1.1
  5.  
  6.     There are no restrictions on this code, it's in the public domain.  I accept
  7.     no responsibility for it's fitness for any particular use.
  8. ***/
  9.  
  10.  
  11. #import "GradientView.h"
  12. #import "Thinker.h"
  13. #import <math.h>
  14. #import <libc.h>
  15. #import <dpsclient/wraps.h>
  16.  
  17. #define STEPS  256   /* number of gradation steps */
  18. #define LEN    256   /* length of a string, as in the default database strings */
  19.  
  20. @implementation GradientView
  21.  
  22. - initFrame:(const NXRect *)rect
  23. {
  24.     [super initFrame: rect];
  25.  
  26.     topColor = NX_COLORBLUE;
  27.     bottomColor = NXChangeBrightnessComponent(NX_COLORBLUE, 0.1);
  28.  
  29.     [self readTopColor: self];
  30.     [self readBottomColor: self];
  31.  
  32.     aColorWellHasChanged = YES;
  33.     doingScreenSaver = NO;
  34.  
  35.     return self;
  36. }
  37.  
  38. - oneStep
  39. {
  40.     usleep(1000000 * 1/2 );            //sleep a while (yuck!)
  41.     // this time should be short enough that BackSpace remains
  42.     // somewhat responsive if an event does occur.  Unfortunately,
  43.     // waking up keeps BackSpace hotter than I would prefer (ie paged in)
  44.  
  45.     return self;
  46. }
  47.  
  48. - drawSelf:(const NXRect *)rects :(int)rectCount
  49. {
  50.     static int i;
  51.     static float stepWidth;
  52.     static NXRect fillArea;
  53.     static NXColor currentFill;
  54.     static float redInc, greenInc, blueInc;
  55.     static float redValue, greenValue, blueValue;
  56.  
  57.     /* if we're in screen saver mode, then just black out the screen */
  58.  
  59.     if (doingScreenSaver)
  60.     {
  61.         PSsetgray(NX_BLACK);
  62.         NXRectFill(&bounds);
  63.         return self;  /* don't do anything, leave it black */
  64.     }
  65.  
  66.     /* We're doing the gradient, calculate the width of each step */
  67.  
  68.     stepWidth = bounds.size.height/STEPS;
  69.     bcopy(&bounds, &fillArea, sizeof(NXRect));
  70.     fillArea.size.height = stepWidth;
  71.  
  72.     bcopy(&bottomColor, ¤tFill, sizeof(NXColor));
  73.  
  74.     /* We're going to do a linear interpolation between each component */
  75.  
  76.     redValue = NXRedComponent(currentFill);
  77.     greenValue = NXGreenComponent(currentFill);
  78.     blueValue = NXBlueComponent(currentFill);
  79.  
  80.     if (aColorWellHasChanged)
  81.     {
  82.         /* Calculate the increment value, this has only change if the color wells changed */
  83.  
  84.         redInc = (NXRedComponent(topColor) - redValue)/STEPS;
  85.         greenInc = (NXGreenComponent(topColor) - greenValue)/STEPS;
  86.         blueInc = (NXBlueComponent(topColor) - blueValue)/STEPS;
  87.  
  88.         aColorWellHasChanged = NO;
  89.     }
  90.  
  91.     for (i = 0; i < STEPS; i++)
  92.     {
  93.         NXSetColor(currentFill);
  94.         NXRectFill(&fillArea);
  95.         fillArea.origin.y = stepWidth*(i + 1);
  96.         redValue += redInc;
  97.         greenValue += greenInc;
  98.         blueValue += blueInc;
  99.         currentFill = NXChangeRedComponent(currentFill, redValue);
  100.         currentFill = NXChangeGreenComponent(currentFill, greenValue);
  101.         currentFill = NXChangeBlueComponent(currentFill, blueValue);
  102.     }
  103.  
  104.     return self;
  105. }
  106.  
  107. - (BOOL)isBoringScreenSaver
  108. {
  109.     return YES;
  110. }
  111.  
  112. - getBottomColor: sender
  113. {
  114.     bottomColor = [bottomColorWell color];
  115.  
  116.     return self;
  117. }
  118.  
  119. - getTopColor: sender
  120. {
  121.     topColor = [topColorWell color];
  122.  
  123.     return self;
  124. }
  125.  
  126. - apply: sender
  127. {
  128.     aColorWellHasChanged = YES;
  129.  
  130.     [self getBottomColor: self];
  131.     [self getTopColor: self];
  132.  
  133.     [self writeColors: self];
  134.  
  135.     [self update];
  136.  
  137.     return self;
  138. }
  139.  
  140. - inspector: sender
  141. {
  142.     char buf[MAXPATHLEN];
  143.  
  144.     if (!inspectorPanel)
  145.     {
  146.         sprintf(buf, "%s/gradient.nib", [sender moduleDirectory: "Gradient"]);
  147.         [NXApp loadNibFile: buf owner: self withNames: NO];
  148.  
  149.         [topColorWell setColor: topColor];
  150.         [bottomColorWell setColor: bottomColor];
  151.     }
  152.  
  153.     return inspectorPanel;
  154. }
  155.  
  156. - (BOOL) useBufferedWindow
  157. {
  158.     return YES;
  159. }
  160.  
  161. - writeColors: sender
  162. {
  163.     [self writeTopColor: self];
  164.     [self writeBottomColor: self];
  165.  
  166.     return self;
  167. }
  168.  
  169. - writeTopColor: sender
  170. {
  171.     static char str[LEN];
  172.  
  173.     sprintf(str, "%1.5f %1.5f %1.5f",
  174.             NXRedComponent(topColor),
  175.             NXGreenComponent(topColor),
  176.             NXBlueComponent(topColor));
  177.  
  178.     NXWriteDefault([NXApp appName], "Gradient top color", str);
  179.  
  180.     return self;
  181. }
  182.  
  183. - readTopColor: sender
  184. {
  185.     static const char *str;
  186.     static float r,g,b;
  187.  
  188.     if ((str = NXReadDefault([NXApp appName], "Gradient top color")) == NULL)
  189.         return nil;
  190.  
  191.     if (sscanf(str, "%f%f%f", &r, &g, &b) != 3)
  192.     {
  193.         NXLogError("Could not read default for top color.\n");
  194.         return nil;
  195.     }
  196.  
  197.     topColor = NXChangeRedComponent(topColor, r);
  198.     topColor = NXChangeGreenComponent(topColor, g);
  199.     topColor = NXChangeBlueComponent(topColor, b);
  200.  
  201.     return self;
  202. }
  203.  
  204. - writeBottomColor: sender
  205. {
  206.     static char str[LEN];
  207.  
  208.     sprintf(str, "%1.5f %1.5f %1.5f",
  209.             NXRedComponent(bottomColor),
  210.             NXGreenComponent(bottomColor),
  211.             NXBlueComponent(bottomColor));
  212.  
  213.     NXWriteDefault([NXApp appName], "Gradient bottom color", str);
  214.  
  215.     return self;
  216. }
  217.  
  218. - readBottomColor: sender
  219. {
  220.     static const char *str;
  221.     static float r,g,b;
  222.  
  223.     if ((str = NXReadDefault([NXApp appName], "Gradient bottom color")) == NULL)
  224.         return nil;
  225.  
  226.     if (sscanf(str, "%f%f%f", &r, &g, &b) != 3)
  227.     {
  228.         NXLogError("Could not read default for bottom color.\n");
  229.         return nil;
  230.     }
  231.  
  232.     bottomColor = NXChangeRedComponent(bottomColor, r);
  233.     bottomColor = NXChangeGreenComponent(bottomColor, g);
  234.     bottomColor = NXChangeBlueComponent(bottomColor, b);
  235.  
  236.     return self;
  237. }
  238.  
  239. - enteredScreenSaverMode
  240. {
  241.     doingScreenSaver = YES;
  242.     [self update];
  243.  
  244.     return self;
  245. }
  246.  
  247. - willExitScreenSaverMode
  248. {
  249.     doingScreenSaver = NO;
  250.     [self update];
  251.  
  252.     return self;
  253. }
  254.  
  255. @end
  256.